package com.lottery.im.widget.floatwindow;

import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

/**
 */

public class Util {

  static View inflate(Context applicationContext, int layoutId) {
    LayoutInflater inflate =
        (LayoutInflater) applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    return inflate.inflate(layoutId, null);
  }

  private static Point sPoint;

  public static int getScreenWidth(Context context) {
    if (sPoint == null) {
      sPoint = new Point();
      WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
      wm.getDefaultDisplay().getSize(sPoint);
    }
    return sPoint.x;
  }

  public static int dp2px(Context context, float dpVal) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal,
        context.getResources().getDisplayMetrics());
  }

  static int getScreenHeight(Context context) {
    if (sPoint == null) {
      sPoint = new Point();
      WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
      wm.getDefaultDisplay().getSize(sPoint);
    }
    return sPoint.y;
  }

  static int getStatusBarHeight(Context context) {
    int statusBarHeight = 0;
    //获取status_bar_height资源的ID
    int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
      //根据资源ID获取响应的尺寸值
      statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
    }
    return statusBarHeight;
  }

  static boolean isViewVisible(View view) {
    return view.getGlobalVisibleRect(new Rect());
  }
}
